var express = require('express'),
	db = require('../pubModule/mysql'),
	encode = require('../pubModule/crypto'),
	log = express();

log.get('/',function(req, res){
	res.render('log',{})
})

log.post('/',function(req, res){
	var userName = req.body.name,
		userPassword = req.body.password;
	var query = 'select * from users where name = "' + userName + '"';
	db.poolSelectOne(query,function(err,row){
		if (checkPassword(userPassword, row[0].password)) {
			req.session.userName = row[0].name
			req.session.userPassword = row[0].password
			var enUserName = encode.encrypto(row[0].name)
			res.cookie('userName', enUserName, {expires: new Date(Date.now() + 1000*7*24*60*60), httpOnly: true})
			res.redirect(301,'/')
		} else {
			res.render('log',{})
		}
	})
})

/*
*	没什么意义的function
*/
function checkPassword(userPassword, selectPassword){
	if (userPassword == selectPassword) {
		return true;
	} else {
		return false;
	}
}

module.exports = log